package com.LeeCode;

/**
 * 奇偶链表
 */

public class Code328 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        ListNode head = Utils.arrayToListNode(arr);
        Utils.printListNode(new Code328().oddEvenList(head));
    }

    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummy = new ListNode(0);
        ListNode cur1 = head, cur2 = dummy;

        // 跳到最后一个奇数节点
        while (cur1.next != null && cur1.next.next != null) {
            cur2.next = cur1.next;
            cur2 = cur2.next;
            cur1.next = cur1.next.next;
            cur1 = cur1.next;
        }
        cur2.next = cur1.next;
        cur1.next = dummy.next;
        return head;
    }
}
